/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package we.dispatcher.packet;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import we.config.CConstant;
import we.dispatcher.CPacketWriter;
import we.dispatcher.writer.IWritingProcessor;

/**
 *
 * @author ADMIN
 */
public abstract class CSentPacket
{

    protected boolean valid;
    protected int id;
    protected CPacketWriter writer;
    private List<CPacketWriter.WriterPair> pairs;

    public CSentPacket(int id)
    {
        this.id = id;
        valid = true;
        writer = new CPacketWriter(CConstant.MAX_MESSAGE_LENGTH);
        pairs = new ArrayList<CPacketWriter.WriterPair>();
    }

    public boolean isValid()
    {
        return valid;
    }

    public ByteBuffer getBuffer()
    {
        return writer.buffer();
    }

    public abstract void processData();

    final protected void addPair(IWritingProcessor processor, Object obj)
    {
        pairs.add(new CPacketWriter.WriterPair(processor, obj));
    }

    final protected void processAuto()
    {
        writer.writeByProcessor(id, pairs.toArray(new CPacketWriter.WriterPair[pairs.size()]));
    }
}
